{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 复杂文件夹的复制、移动、删除"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###### 1、单个文件的拷贝"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import shutil"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T14:12:56.752688300Z",
     "start_time": "2023-06-26T14:12:56.741688700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "'这是测试文件.txt'"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (1) 将文件拷贝到文件夹下\n",
    "shutil.copy('../Test/这是测试文件.txt','.')\n",
    "# (2) 将文件拷贝到指定文件路径，若已存在，会被替换\n",
    "shutil.copy('../Test/这是测试文件.txt', '这是测试文件.txt')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T15:52:28.456065600Z",
     "start_time": "2023-06-26T15:52:28.417238400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###### 2、目录树的拷贝"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "'./Test'"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#shutil.copytree(old_path,new_path)\n",
    "# new_path目录路径不能存在，否则报FileExistsError\n",
    "# 将old_path文件夹下所有内容复制到new_path下，但不包括old_path文件夹本身\n",
    "# new_path路径不存在会递归创建，如\"./a/b/c\"\n",
    "# Python版本>=3.8, 新增设置dirs_exist_ok=True，则允许new_path存在\n",
    "shutil.copytree('../Test', './Test')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T15:17:55.346168600Z",
     "start_time": "2023-06-26T15:17:55.297614Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###### 3、文件和文件夹的移动和重命名"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "'./b'"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 移动文件夹\n",
    "# shutil.move(old_path, new_path)\n",
    "# 注意:\n",
    "# (1) new_path不存在，自动创建，并将old_path内的所有内容移动到new_path下，不包括old_path文件夹本身\n",
    "# (2) new_path存在，则连old_path文件夹本身也移动到该目录下\n",
    "shutil.move('../Test02', './b')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T16:18:44.191015400Z",
     "start_time": "2023-06-26T16:18:44.142977400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "'C:\\\\Users\\\\17634\\\\Desktop\\\\红楼梦.txt'"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 移动文件\n",
    "# (1) 将文件移动到目录下\n",
    "shutil.move('红楼梦.txt', './a')\n",
    "# (2) 文件重命名\n",
    "shutil.move('./a/红楼梦.txt', r'C:\\Users\\17634\\Desktop\\西游记.txt')\n",
    "# (3) 文件内容替换，用old_path的文件内容替换掉new_path的文件内容\n",
    "shutil.move('红楼梦.txt', './a/List.py')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T15:29:58.099740200Z",
     "start_time": "2023-06-26T15:29:58.019218900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###### 4、文件和文件夹的删除"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [],
   "source": [
    "# 文件删除\n",
    "# p = Path('./b/c/List.py')\n",
    "# p.unlink()\n",
    "\n",
    "# 文件夹永久删除\n",
    "shutil.rmtree('./b')\n",
    "\n",
    "# 文件夹删除到回收站\n",
    "#pip install send2trash\n",
    "import send2trash\n",
    "# 删除文件或文件夹\n",
    "send2trash.send2trash('./a') # 将a文件夹及其下所有内容移动到回收站\n",
    "# send2trash.send2trash('./这是测试.txt')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T15:33:19.907324600Z",
     "start_time": "2023-06-26T15:33:19.774377300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import torch\n",
    "w = torch.tensor([1.0])  # tensor定义初始值\n",
    "w.requires_grad = True  # 需要计算梯度\n",
    "w.backward()"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
